package net.pimpas.web.tests.bb;

import javax.faces.event.ActionEvent;

import net.pimpas.framework.PimpasFramework;
import net.pimpas.persistence.hibernate.serviceprovider.HibernateServiceProvider;
import net.pimpas.persistence.hibernate.serviceprovider.HibernateServiceProviderFactoryImpl;
import net.pimpas.web.jsf.components.tree.TreeNode;
import net.pimpas.web.jsf.components.tree.TreeNodeUtils;
import net.pimpas.web.jsf.managedbean.ManagedBeanAnnotation;

import org.hibernate.Session;
import org.hibernate.SessionException;
import org.hibernate.Transaction;
import org.hibernate.TransactionException;

@ManagedBeanAnnotation(beanName="teste")
public class TesteBB {

	private String value;
	private String email;
	private String emailRegex;
	
	public TesteBB() {
		this.setValue("");
		this.setEmail("");
		this.setEmailRegex("(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*");
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public void doSomething(ActionEvent evt) {
		HibernateServiceProvider per = HibernateServiceProviderFactoryImpl.getPersistenceServiceProvider();
		try {
			Session session = per.getSession(PimpasFramework.getCurrentInstance().getContext());
			Transaction trans = session.beginTransaction();
			System.out.println(trans.isActive());
			session.close();
			System.out.println(session.isConnected());
		} catch (SessionException e) {
			e.printStackTrace();
		} catch (TransactionException e) {
			e.printStackTrace();
		}
	}
	
	public TreeNode processTree() {
		BeanTeste bean01 = new BeanTeste();
		bean01.setId(1);
		bean01.setNome("bean01");
		TreeNode node01 = TreeNodeUtils.createNode(bean01, "nome", "id");
		BeanTeste bean02 = new BeanTeste();
		bean02.setId(2);
		bean02.setNome("bean02");
		TreeNode node02 = TreeNodeUtils.createNode(bean02, "nome", "id");
		node01.getChildren().add(node02);
		
		return node01;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmailRegex() {
		return emailRegex;
	}

	public void setEmailRegex(String emailRegex) {
		this.emailRegex = emailRegex;
	}
}